﻿using Dapper;
using IntelligentGateway.Model;
using IntelligentGateway.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace IntelligentGateway.Data
{
    public class SignalPointDA
    {
        public SignalPoint GetSignalPointData(int FloorID, int TypeID, bool DFloor)
        {
            string strSql = string.Empty;
            if (DFloor)
            {
                strSql = "SELECT * FROM signalpoint Where DFloorID=@FloorID and TypeID=@TypeID";
            }
            else
                strSql = "SELECT * FROM signalpoint Where FloorID=@FloorID and TypeID=@TypeID";
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    SignalPoint point = conn.QueryFirst<SignalPoint>(strSql, new { FloorID = FloorID, TypeID = TypeID });
                    return point;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(int FloorID,int TypeID):" + e.ToString());
#endif
                    return null;
                }
            }
        }
        public SignalPoint GetSignalPointData(string Name)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    SignalPoint point = conn.QueryFirst<SignalPoint>("SELECT * FROM signalpoint Where Name=@Name", new { Name = Name });
                    return point;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(string Name):" + e.ToString());
#endif
                    return null;
                }
            }
        }

        public SignalPoint GetSignalPointData(string Name,int deviceid)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    SignalPoint point = conn.QueryFirst<SignalPoint>("SELECT * FROM signalpoint Where Name=@Name AND `NO`=(SELECT SignType FROM masterdeviceinfo WHERE `NO`=@NO)", new { Name = Name,NO=deviceid });
                    return point;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(string Name):" + e.ToString());
#endif
                    return null;
                }
            }
        }

        public SignalPoint GetSignalPointPFloorData(int TypeID, string DFloorID, string DeviceNO)
        {
            string strSql = string.Format("SELECT * FROM signalpoint Where DFloorID='{0}' and TypeID={1} and NO=((SELECT SignType FROM masterdeviceinfo WHERE `NO`={2}))", DFloorID, TypeID, DeviceNO);
            Log.Debug("查询:" + strSql);
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    SignalPoint point = conn.QueryFirst<SignalPoint>(strSql);
                    return point;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(int FloorID,int TypeID):" + e.ToString());
#endif
                    return null;
                }
            }
        }

        public List<SignalPoint> GetSignalPFloorPointListData(int TypeID, string PFloorID, string DeviceNO)
        {
            string strSql = string.Format("SELECT * FROM signalpoint Where FloorID in({0}) and TypeID={1} and NO=((SELECT SignType FROM masterdeviceinfo WHERE `NO`={2}))", PFloorID, TypeID, DeviceNO);
            Log.Debug("查询:" + strSql);
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<SignalPoint> infos = conn.Query<SignalPoint>(strSql).AsList();
                    return infos;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(int FloorID,int TypeID):" + e.ToString());
#endif
                    return null;
                }
            }
        }

        public List<SignalPoint> GetSignalDFloorPointListData(int TypeID, string DFloorID, string DeviceNO)
        {
            string strSql = string.Format("SELECT * FROM signalpoint Where DFloorID ='{0}' and TypeID={1} and NO=((SELECT SignType FROM masterdeviceinfo WHERE `NO`={2}))", DFloorID, TypeID, DeviceNO);
            Log.Debug("查询:" + strSql);
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<SignalPoint> infos = conn.Query<SignalPoint>(strSql).AsList();
                    return infos;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(int FloorID,int TypeID):" + e.ToString());
#endif
                    return null;
                }
            }
        }

        public List<SignalPoint> GetAllPointListData(string name)
        {
            string strSql = string.Format("SELECT * FROM signalpoint Where Name like '%{0}%'", name);

            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<SignalPoint> infos = conn.Query<SignalPoint>(strSql).AsList();
                    return infos;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(int FloorID,int TypeID):" + e.ToString());
#endif
                    return null;
                }
            }
        }

        public List<SignalPoint> GetAllPointType()
        {
            string strSql = string.Format("SELECT DISTINCT(no) FROM signalpoint");

            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<SignalPoint> infos = conn.Query<SignalPoint>(strSql).AsList();
                    return infos;
                }
                catch (Exception e)
                {
#if DEBUG
                    Log.Debug("GetSignalPointData(int FloorID,int TypeID):" + e.ToString());
#endif
                    return null;
                }
            }
        }
    }
}
